#include "commonutils.h"
#include <QDebug>
#include <QUrl>
#include <QPair>
#include <QtXml>

CommonUtils::CommonUtils()
{
}

QString CommonUtils::getTagContaining(QString page, QString tag, QString content){
    int begPos=0,begPos2=0;
    int lastPos=0;
    begPos=page.indexOf("<"+tag);
    while(begPos!=-1){
        lastPos=page.indexOf(">",begPos);
        if(page[lastPos-1]!='/')
            lastPos=page.indexOf("</"+tag+">",begPos);
        if(lastPos<0){
            qDebug()<<"Niezamkniety tag <"+tag+">!!";
            qDebug()<<page.right(begPos);
            return NULL;
        }
        begPos2=page.indexOf("<"+tag,begPos+1);
        while((begPos2<lastPos)&&(begPos2>0)){
            begPos2=page.indexOf("<"+tag,begPos2+1);
            lastPos=page.indexOf("</"+tag+">",lastPos+1);
            if(lastPos<0){
                qDebug()<<"Niezamkniety tag <"+tag+">!!";
                qDebug()<<page.right(begPos);
                return NULL;
            }
        }
        QString wycinek=page.mid(begPos+1+tag.length(),lastPos-begPos);
        if(wycinek.contains(content)){
            QString rekWycinek=getTagContaining(wycinek, tag, content);
            if(rekWycinek.isEmpty()){
                return wycinek;//nie ma mniejszego kawalka zawierajacego kontent
            }else{
                return rekWycinek;
            }
        }
        begPos=begPos2;
    }
    return "";
};

QString *CommonUtils::getFormPath(QString page){
    QRegExp reg("action=\"([^\"]*)");
    reg.indexIn(page);
    return new QString(reg.cap(1));
}

QString *CommonUtils::getFormPost(QString page){
    QRegExp inputy("<input ([^>]*)");
    QString post;
    int pos=0;
    QString line("");
    while((pos=inputy.indexIn(page,pos))>=0){
        line.clear();
        line.append(inputy.cap(1));
            pos+=line.length();
        if(!line.contains("hidden")) continue;


        QRegExp reg("name=([^\\s]*)");
        reg.indexIn(line);
        post.append(reg.cap(1));
        post.append("=");
        QRegExp reg2("value=([^\\s]*)");
        reg2.indexIn(line);
        post.append(reg2.cap(1));
        post.append("&");



    }
    return new QString(post.left(post.length()-1));
};

QString* CommonUtils::getAddressGet(QString urlParam){
    QUrl url(urlParam);
    if(!url.isValid())return NULL;
    QString *ret=new QString();
    QPair<QString, QString> pair;
    foreach(pair, url.queryItems()){
        ret->append("&");
        ret->append(pair.first);
        ret->append("=");
        ret->append(pair.second);
    }
    if(ret->length()>1){
        ret->replace(0,1,"?");
    }

    return ret;
};


QString CommonUtils::getSingleTagValue(QDomNode node, QString name){
    QDomNode tag=node.namedItem(name);
    if(tag.isNull()){
        qDebug()<<"Could not find tag "<<name;
    };
    QDomElement el=tag.toElement();
    if(el.isNull()){
        qDebug()<<name<<" is not element";
    }
    return el.text();

};

QString CommonUtils::getSingleAttributeValue(QDomNode node, QString name){
    QDomNode act=node.namedItem(name);
    if(!act.hasAttributes()){
        qDebug()<<"No attributes with "<<name;
        return "";
    }
    if(act.attributes().length()!=1){
        qDebug()<<"Invalid number of "<<name<<" attributes: "<<act.attributes().length();
        return "";
    }

    return QString(act.attributes().item(0).toAttr().value());

};
